package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.TimeUtils;
import com.sevenGroup.pawnshop.controller.form.ValuationsForm;
import com.sevenGroup.pawnshop.controller.info.ValuationsInfo;
import com.sevenGroup.pawnshop.dao.GoodsDao;
import com.sevenGroup.pawnshop.model.Valuations;
import com.sevenGroup.pawnshop.dao.ValuationsDao;
import com.sevenGroup.pawnshop.service.ValuationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class ValuationsServiceImpl extends ServiceImpl<ValuationsDao, Valuations> implements ValuationsService {
    @Resource
    private ValuationsDao valuationsDao;
    @Resource
    private GoodsDao goodsDao;

//  新增估价表
    @Override
    @Transactional
    public Integer addValuation(ValuationsForm valuationsForm) {
//        先转换格式,并获取当前时间
        Valuations valuations = CopyProperties.copyOne(valuationsForm, Valuations.class);
        valuations.setValuationTime(TimeUtils.getNowTimeString());
//        调用dao新增评估表
        int insert = valuationsDao.insert(valuations);
//        根据ID修改商品表
        goodsDao.updateGoodState(valuationsForm.getGoodId(),"已评估");
        return insert;
    }

//  根据商品ID查询估计表集合
    @Override
    public List<ValuationsInfo> selectValuationByGoodId(Integer goodId) {
        List<Valuations> valuations = valuationsDao.selectList(new QueryWrapper<Valuations>().eq("good_id", goodId));
//        转换成前端的info格式
        return CopyProperties.copyList(valuations,ValuationsInfo.class);
    }
}
